package com.ztjy.sys.controller;

import com.github.pagehelper.PageInfo;
import com.ztjy.annonation.MyLog;
import com.ztjy.bean.Dept;
import com.ztjy.bean.Users;
import com.ztjy.sys.biz.AreaBiz;
import com.ztjy.sys.biz.DeptBiz;
import com.ztjy.sys.biz.UsersBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

/**
 * 部门管理
 */
@Controller
@RequestMapping("/sys/dept")
public class DeptController {
    @Autowired
    private DeptBiz deptBiz;
    @Autowired
    private AreaBiz areaBiz;
    @Autowired
    private UsersBiz usersBiz;

    @ModelAttribute
    public void initModuleData(HttpSession session) {
        session.removeAttribute("mid");
        session.setAttribute("mid", 26);
    }

    @MyLog("部门查询所有的操作")
    @RequestMapping("/deptList")
    public String deptall(Model model, Dept dept, @RequestParam(value = "pageno", defaultValue = "1") Integer pageNo) {
        Integer pageSize = 6;
        PageInfo<Dept> allPage = deptBiz.findAllPage(dept, pageNo, pageSize);
        model.addAttribute("dpInfo", allPage);
        model.addAttribute("dept",dept);
        return "forward:/sys/dept/deptList.html";
    }

    @MyLog("部门添加操作")
    @RequestMapping("/addDept")
    public String addDept(Dept dept){
        boolean flag = deptBiz.addDept(dept);
        if(flag){
            return "redirect:/sys/dept/deptList";
        }else {
            return "forward:/sys/users/addDept.html";
        }
    }

    @RequestMapping("/goUpdate/{deptId}")
    public String goUpdateDept(@PathVariable("deptId")Integer id,Model model){
        Dept dept = deptBiz.findDeptById(id);
        model.addAttribute("dept",dept);
        return  "forward:/sys/dept/deptUpdate.html";
    }

    @RequestMapping("/doUpdate")
    public String doUpdateDept(Dept dept){
        boolean flag = deptBiz.updateDept(dept);
        if(flag){
            return "redirect:/sys/dept/deptList";
        }else {
            return "forward:/sys/users/deptUpdate.html";
        }
    }

    /**
     * 修改状态
     */
    @RequestMapping("/updatestate")
    public String updateState(Model model, Integer id, String chagestate){
        Dept dept = deptBiz.findDeptById(id);
       dept.setDeptState(chagestate);
       deptBiz.updateDept(dept);
        return "redirect:/sys/dept/deptList";
    }

}
